R began as a statistics program, and is still used as one by many users. At a simple level you can type in “3 + 4”, press return, and R will respond “7”. Try the example below. Code for you to type into R is shown like this:
3 + 4
Then R’s output is shown like this:
[1] 7
R has developed into a GIS as a result of user contributed packages, or libraries, as R refers to them. We will be using several libraries in this practical, and will load them as necessary.
If you are using this worksheet outside of the course, you may need to install the R libraries as well as loading them. To do this, run "install.package("package_name").
We won’t spend too much time on the basics of using R - if you want to find out more, there are some good tutorials at http://www.social-statistics.org/?p=764 or http://rpubs.com/nickbearman/gettingstartedwithr.
We are going to use a program called R Studio, which works on top of R and provides a good user interface. I’ll talk a little bit about it in the presentation, but the key areas of the window are these:
Open up R Studio (click Start > All Programs > R Studio > R Studio) and arrange the windows so you can see the instructions in the web browser along side R Studio.
One of the libraries we will use is ggmap, which allows us to quickly and easily include some Google Maps within R. First, we need to load the library, so type into the console:
library(ggmap)
This will load various other libraries and you will probably see some black information messages. If you see any red error messages, then ask for help.
We can then create a quick map of Liverpool with the qmap() function:
qmap('Liverpool')
We can also search for anything we would search for in the Google Maps search box - e.g. different locations, or postcodes. For example:
qmap('CH1 1AA')
Try different postcodes or locations to search for, and see what happens. You will see that the scale of the map stays the same - this is fine for Liverpool, but of limited use for somewhere smaller (like Norwich) or larger (like Ireland). The default zoom is level 10, but we can change this to something more useful. Higher numbers zoom in more (i.e. show a smaller area). Try:
qmap('L69 3GP', zoom = 16)
This shows us the map centred on the Jane Herdman building (although it isn’t marked on the map).
As well as the maps layer, we can access the satellite and hybrid map types from Google Maps:
qmap('L69 3GP', zoom = 16, maptype = 'satellite')
One useful shortcut when using R Studio (or R on its own) is that pressing the ‘up’ key on the keyboard will show your previous command. You can then edit this and press return to run it, which avoids the need to type the whole command out again! Try it with the command below:
qmap('L69 3GP', zoom = 16, maptype = 'hybrid')